﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using IdCardReaderHelper.Common;
using IdCardReaderHelper.Models;

namespace IdCardReaderHelper
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            IDCardData cardData = new IDCardData();
            var status = IdCardReader.Read(ref cardData);
            img.Source = null;
            ResultTxt.Text = string.Empty;
            switch (status)
            {
                case 0:
                    string text = string.Format(@"
姓名：{0}
身份证号：{5}
性别：{1}
名族：{2}
出生日期：{3}
住址：{4}
发证机关：{6}
有效开始日期：{7}
有效结束日期：{8}
", cardData.Name,
                        cardData.Sex,
                        cardData.Nation,
                        cardData.Born,
                        cardData.Address,
                        cardData.IDCardNo,
                        cardData.GrantDept,
                        cardData.UserLifeBegin,
                        cardData.UserLifeEnd);
                    ResultTxt.Text = text;
                   Bitmap bit=new Bitmap(IdCardReader.GetImgPath(cardData.IDCardNo));
                    var newBit = CopyBitmap(bit);
                    bit.Dispose();

                    img.Source = BitmapToBitmapSource(newBit);// BitmapFrame.Create()
                    //img.Source=new BitmapImage(new Uri(IdCardReader.GetImgPath(cardData.IDCardNo),UriKind.RelativeOrAbsolute));
                        //new BitmapImage(new Url());
                    break;
                default:
                    ResultTxt.Text = IdCardReader.GetStatusMsg(status);
                    break;
            }
        }
        private static Bitmap CopyBitmap(Bitmap map)
        {
            int width = map.Width;
            int height = map.Height;
            Bitmap bit = new Bitmap(width, height);
            using (var g = Graphics.FromImage(bit))
            {
                g.DrawImage(map, 0, 0, width, height);
            }
            return bit;
        }
        [System.Runtime.InteropServices.DllImport("gdi32.dll")]
        private static extern bool DeleteObject(IntPtr hObject);
        /// <summary>
        /// 释放intptr
        /// </summary>
        /// <param name="hObject"></param>
        public static void DisposeIntPtrObject(IntPtr hObject)
        {
            DeleteObject(hObject);
        }
        /// <summary>
        /// 将bitmap转换为imagesource
        /// </summary>
        /// <param name="map"></param>
        /// <returns></returns>
        public static BitmapSource BitmapToBitmapSource(Bitmap map)
        {
            IntPtr hBitmap = map.GetHbitmap();
            BitmapSource bitmapSource =
                Imaging.CreateBitmapSourceFromHBitmap(
                    hBitmap, IntPtr.Zero, Int32Rect.Empty,
                    BitmapSizeOptions.FromEmptyOptions());
            DeleteObject(hBitmap);
            return bitmapSource;
        }
    }
}
